<template>
  <div class="">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>就诊人管理</span>
          <el-button type="primary" @click="addUser">
            <el-icon><User /></el-icon>添加就诊人</el-button
          >
        </div>
      </template>
      <!-- 就诊人信息 -->
      <div class="patient-box" v-if="scene == 0">
        <Patient
          v-for="(item, index) in userList"
          :key="item.id"
          :item="item"
          :index="index"
          @change="changeScene"
          @remove="removeUser"
        >
        </Patient>
      </div>
      <!-- 添加修改就诊人信息解构 -->
      <div class="form" v-else>
        <el-form label-width="80" style="width: 60%; margin: 10px auto">
          <el-divider content-position="left">就诊人信息</el-divider>
          <el-form-item label="用户姓名">
            <el-input
              placeholder="请输入用户姓名"
              v-model="userParams.name"
            ></el-input>
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select
              placeholder="请选择证件类型"
              v-model="userParams.certificatesType"
            >
              <el-option
                v-for="item in ertificationType"
                ::key="item.id"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="证件号码">
            <el-input
              placeholder="请输入证件号码"
              v-model="userParams.certificatesNo"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户性别">
            <el-radio-group v-model="userParams.sex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="出生日期">
            <el-date-picker
              type="date"
              placeholder="请选择出生日期"
              v-model="userParams.birthdate"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input
              placeholder="请输入手机号码"
              v-model="userParams.phone"
            ></el-input>
          </el-form-item>
          <el-divider content-position="left"
            >建档信息（完善后部分医院首次就诊不排队建档）</el-divider
          >
          <el-form-item label="婚姻状态">
            <el-radio-group v-model="userParams.isMarry">
              <el-radio :label="1">已婚</el-radio>
              <el-radio :label="0">未婚</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="自费/医保">
            <el-radio-group v-model="userParams.isInsure">
              <el-radio :label="1">自费</el-radio>
              <el-radio :label="0">医保</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="当前地址">
            <el-cascader v-model="userParams.addressSelected" :props="props" />
          </el-form-item>
          <el-form-item label="详细地址">
            <el-input
              placeholder="请输入详细地址"
              v-model="userParams.address"
            ></el-input>
          </el-form-item>
          <el-divider content-position="left">联系人信息(选填)</el-divider>
          <el-form-item label="用户姓名">
            <el-input
              placeholder="请输入用户姓名"
              v-model="userParams.contactsName"
            ></el-input>
          </el-form-item>
          <el-form-item label="证件类型">
            <el-select
              placeholder="请选择证件类型"
              v-model="userParams.contactsCertificatesType"
            >
              <el-option
                v-for="item in ertificationType"
                ::key="item.id"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="证件号码">
            <el-input
              placeholder="请输入证件号码"
              v-model="userParams.contactsCertificatesNo"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input
              placeholder="请输入手机号码"
              v-model="userParams.contactsPhone"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button @click="reset()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from "vue";
import { reqGetUser } from "@/api/hospital/index";
import type { UserArr, UserResponseData } from "@/api/hospital/type";
import { useRoute, useRouter } from "vue-router";
import {
  reqCertificationType,
  reqCity,
  reqAddOrUpdateUser,
} from "@/api/user/index";
import type {
  CertationArr,
  CertationTypeResponseData,
  AddOrUpdateUser,
} from "@/api/user/type";
import { User } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
let $route = useRoute();
let $router = useRouter();
let ertificationType = ref<CertationArr>([] as CertationArr); // 身份证件类型

let scene = ref<number>(0); // 卡片身体展示内容 0用户展示 1添加
let userList = ref<UserArr>([]); // 存儲病人列表

let userParams = reactive<AddOrUpdateUser>({
  name: "",
  certificatesType: "",
  certificatesNo: "",
  sex: 0,
  birthdate: "",
  phone: "",
  isMarry: 0,
  isInsure: 0,
  addressSelected: [],
  address: "",
  contactsName: "",
  contactsCertificatesType: "",
  contactsCertificatesNo: "",
  contactsPhone: "",
}); //新增就诊人的数据

onMounted(() => {
  fetchUserData(); //获取病人数据
  fetchCertificationType(); //获取身份证件类型
  // 从预约挂号过来 带有query参数 添加用户 场景1 表单添加
  if ($route.query.type == "add") {
    scene.value = 1;
  }
  if ($route.query.type == "edit") {
    scene.value = 1;
  }
});
// 获取就诊人信息
const fetchUserData = async () => {
  const result: UserResponseData = await reqGetUser();
  if (result.code == 200) {
    userList.value = result.data;
  }
};
// 添加就诊人
const addUser = () => {
  scene.value = 1;
  // 清空上一次数据
  reset();
};
const reset = () => {
  Object.assign(userParams, {
    id: null,
    name: "",
    certificatesType: "",
    certificatesNo: "",
    sex: 0,
    birthdate: "",
    phone: "",
    isMarry: 0,
    isInsure: 0,
    addressSelected: [],
    address: "",
    contactsName: "",
    contactsCertificatesType: "",
    contactsCertificatesNo: "",
    contactsPhone: "",
  });
};
// 自定义事件
const changeScene = (user: AddOrUpdateUser) => {
  scene.value = 1;
  // 编辑窗口收集信息
  Object.assign(userParams, user);
};
// 获取证件类型
const fetchCertificationType = async () => {
  const result: CertationTypeResponseData = await reqCertificationType();
  console.log(result);
  if (result.code == 200) {
    ertificationType.value = result.data;
  }
};

// 级联选择器提供的数据
const props = {
  lazy: true, //懒加载
  async lazyLoad(node: any, resolve: any) {
    const result = await reqCity(node.data.id || "86"); //默认省份参数86
    // 整理数据
    let showData = result.data.map((item: any) => {
      return {
        id: item.id,
        label: item.name,
        value: item.value,
        leaf: !item.hasChildren,
      };
    });
    // 注入组件需要的数据
    resolve(showData);
  },
};

// 提交  新增或更新
const submit = async () => {
  try {
    await reqAddOrUpdateUser(userParams);
    ElMessage({
      type: "success",
      message: userParams.id ? "更新成功" : "新增成功",
    });
    if ($route.query.type) {
      $router.back();
    } else {
      scene.value = 0;
    }
    fetchUserData();
  } catch (error) {
    ElMessage({
      type: "error",
      message: userParams.id ? "更新失败" : "新增失败",
    });
  }
};

// 监听全部就诊人的数据
watch(
  () => userList.value,
  () => {
    if ($route.query.type == "edit") {
      let user = userList.value.find((item: any) => {
        return item.id == $route.query.id;
      });
      Object.assign(userParams, user);
    }
  }
);
// 删除用户自定义事件
const removeUser = () => {
  fetchUserData(); //获取病人数据
};
</script>
<script lang="ts">
export default {
  name: "UserPatient",
};
</script>
<style scoped lang="scss">
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
::v-deep(.el-card__body .patient-box) {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: wrap;
}
</style>
